// From https://www.freecodecamp.org/news/javascript-debounce-example/
// Lodash function not working withing Taro
export const debounce = (func: Function, timeout = 300) => {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(this, args);
    }, timeout);
  };
};

export const throttle = (func: Function, delay = 300) => {
  let shouldWait = false;

  return (...args) => {
    if (shouldWait) return;

    func(...args);
    shouldWait = true;
    setTimeout(() => {
      shouldWait = false;
    }, delay);
  };
};
